package com.school.common.base;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.school.common.constant.Constant;
import com.school.common.tags.PageBean;
import com.school.common.util.SystemConfUtils;

public class BaseAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1299574915181915750L;

	/** 错误信息 */
	private String errorMessage;

	protected PageBean pageBean = new PageBean();

	private String queryString;

	public String getQueryString() {
		this.queryString = (String) this.getHttpSession().getAttribute(Constant.PAGE_QUERYSTRING);
		return queryString;
	}

	public BaseAction() {
		super();
	}

	public Map getSession() {
		return ActionContext.getContext().getSession();
	}

	public HttpSession getHttpSession() {
		return ServletActionContext.getRequest().getSession();
	}

	public HttpServletRequest getHttpServletRequest() {
		return (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	}

	public HttpServletResponse getHttpServletResponse() {
		return (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
	}
	
	public ServletContext getServletContext(){
		return (ServletContext) ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	/**
	 * 根据资源文件中的key值，解析其都对应的值，获得具有键值结构的数据
	 * 
	 * @param i18nKey
	 * @return
	 */
	public Map<String, String> getI18nMap(String i18nModel) {
		HashMap<String, String> result = new LinkedHashMap<String, String>();
		String i18nKey = getText(i18nModel + ".key");
		String i18nValue = getText(i18nModel + ".value");

		if (i18nKey != null && i18nValue != null) {
			String[] keys = i18nKey.split(",");
			String[] values = i18nValue.split(",");
			if (keys != null && values != null && keys.length == values.length) {
				for (int i = 0; i < keys.length; i++) {
					if (keys[i] != null && values[i] != null) {
						result.put(keys[i], values[i]);
					}
				}
			}
		}

		return result;
	}

	/**
	 * 获取资源文件对应键值
	 * 
	 * @param i18nModel
	 * @param key
	 * @return
	 */
	public String getI18nValue(String i18nModel, String key) {
		Map<String, String> map = getI18nMap(i18nModel);
		if (map.containsKey(key)) {
			return map.get(key);
		} else {
			return key;
		}
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
	public Map getGradeMap() {
		String schooleType = SystemConfUtils.getProperty("system.schoole.type");
		String str = "school." + schooleType + ".grades";
		return this.getI18nMap(str);
	}
}
